package com.example.rg1moviemobileapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MovieInfoActivity extends AppCompatActivity {

    private MovieInfo movieInfo;
    private TextView textViewName;
    private ImageView imageViewImg;
    private EditText editText;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_info);
        Intent intent=getIntent();
        movieInfo=(MovieInfo) intent.getSerializableExtra("movieInfo");
        textViewName=findViewById(R.id.textViewName);
        imageViewImg=findViewById(R.id.imageViewImg);
        button=findViewById(R.id.button);
        editText=findViewById(R.id.editText);
        textViewName.setText(movieInfo.getName());
        editText.setText(movieInfo.getInfo());
        Glide.with(this).load("http://172.28.22.100/drawable/"+movieInfo.getImg()).into(imageViewImg);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl("http://172.28.22.100:8088/movieinfo/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RequestMovieInfo requestMovieInfo=retrofit.create(RequestMovieInfo.class);
                movieInfo.setInfo(editText.getText().toString());
                Call<String> call=requestMovieInfo.updatebyID(movieInfo);
                call.enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        if(response.body().equals("true"))
                        {
                            Toast.makeText(MovieInfoActivity.this,"更新成功！",Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            Toast.makeText(MovieInfoActivity.this,"更新失败！",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {

                    }
                });
            }
        });
    }
}
